package com.foronenet.mud;

public class ParseUtils {
    private static int BASE_SENSOR_VALUE = 65536;
    private static double RESISTIVITY = 150;

    private static double BASE_VOLTAGE = 3.3;

    private static double MAX_AMPERAGE  = 20;//单位：mA
    private static double MIN_AMPERAGE = 4;//单位：mA


    public static byte[] hexStringToBytes(String hex) {
        int len = hex.length();
        byte[] b = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            b[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16));
        }
        return b;
    }
    /**
     * 将16进制的字节数组中的4个元素（offset开始到offset+1），转换为短整型
     */
    public static int bytesToShort(byte[] bytes, int offset) {
        short result = (short) ((int) bytes[offset] & 0xff);
        result |= ((int) bytes[offset + 1] & 0xff) << 8;
        return result & 0xffff;
    }

    public static int getBoxId(int canId){
        return canId >> 2;
    }

    /**
     * 传感器数值转电流（针对电流信号的传感器转换）
     */
    public static double sv2a(int sensorValue){
        return sensorValue * BASE_VOLTAGE * 1000 / BASE_SENSOR_VALUE / RESISTIVITY;
    }
    /**
     * 传感器数值转电压（针对电压信号的传感器转换）
     */
    public static double sv2v(int sensorValue){
        return sensorValue * BASE_VOLTAGE / BASE_SENSOR_VALUE ;
    }
}
